import { createRouter, createWebHistory } from 'vue-router'
import UserManage from '@/components/UserManage.vue'
import RoleManage from '@/components/RoleManage.vue'
import MenuManage from '@/components/MenuManage.vue'
import DepartmentManage from '@/components/DepartmentManage.vue'
import PostManage from '@/components/PostManage.vue'
import login from '@/components/loginLog.vue'
import HomeView from '../views/HomeView.vue'
import OperLog from '@/components/OperLog.vue'
import loginpage from '@/components/loginpage.vue'





const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      name:'logg',
      component:loginpage
    },
    {
      path: '/',
      redirect:'/system',
      name: 'home',
      meta: { requiresAuth: true },
      component: HomeView,
      children:[
        {
          path:"system",
          name:'system',
          children:[
            {
              path: 'sysUser',
              name: '用户管理',
              component: UserManage
            },            {
              path: 'sysRole',
              name: '角色管理',
              component: RoleManage
            },            {
              path: 'sysMenu',
              name: '菜单管理',
              component: MenuManage
            },            {
              path: 'sysDept',
              name: '部门管理',
              component: DepartmentManage
            },            {
              path: 'sysPost',
              name: '岗位管理',
              component: PostManage
            },            {
              path: 'log',
              name: '日志管理',
              children:[
                  {
                    path:'sysOperLog',
                    name: '操作日志',
                    component: OperLog
                  },{
                    path:'sysLoginLog',
                    name: '登录日志',
                    component: login
                  }
                ]
            },
          ]
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 在这里进行需要的逻辑处理
  if (to.meta.requiresAuth && !isAuthenticated()) {
    // 如果需要登录但用户未登录，重定向到登录页面
    next('/login');
  } else {
    next(); // 确保调用 next()，否则路由不会继续
  }
});


function isAuthenticated () {

  return localStorage.getItem('token')!=null ;
}


// // 在需要获取 token 的地方
// function getTokenFromPinia() {
//   // 使用 useAuthStore() 获取 AuthStore 的实例

//   return token;
// }
export default router
